<template>
  <a-modal
    title="修改菜单"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          v-show="!addable"
          label="id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            v-decorator="['roleId']"
            :disabled="editable || showable"
          />
        </a-form-item>
        <a-form-item
          label="角色名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="showable"
            v-decorator="['roleName', {rules: [{required: true, message: '请输入角色名称'}]}]" />
        </a-form-item>
        <a-form-item
          label="角色代码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="editable || showable"
            v-decorator="['roleCode', {rules: [{required: true, message: '请输入角色代码'}]}]" />
        </a-form-item>
        <a-form-item
          label="数据权限"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            @change="selectChange"
            :disabled="showable"
            v-decorator="['dsType', {rules: [{required: true, message: '选择类型'}],initialValue: '2'}]"
            :options="dsTypeList"
          />
        </a-form-item>
        <!--自定义 时需要自己选择权限-->
        <a-form-item
          v-show="dsType === '1'"
          label="数据权限"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-tree-select
            :disabled="showable"
            showSearch
            multiple
            placeholder="请选择权限部门"
            allowClear
            :treeData="treeData"
            treeDefaultExpandAll
            treeNodeFilterProp="title"
            v-decorator="['dsScopeList', {rules: [{required: true, message: '请选择权限部门'}]}]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
    <template slot="footer">
      <a-button key="cancel" @click="handleCancel">取消</a-button>
      <a-button key="forward" :loading="confirmLoading" type="primary" @click="handleOk">保存</a-button>
    </template>
  </a-modal>
</template>

<script>
import { getObj, addObj, putObj } from '@api/sys/role'
import pick from 'lodash.pick'
const dsTypeList = [
  {
    label: '全部',
    value: '0'
  },
  {
    label: '自定义',
    value: '1'
  },
  {
    label: '本级',
    value: '2'
  },
  {
    label: '本级及子级',
    value: '3'
  }

]
export default {
  name: 'RoleAddOrUpdate',
  data () {
    return {
      dsTypeList,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      type: 'add',
      editable: false,
      addable: false,
      showable: false,
      dsType: '2',
      treeData: [
        {
          title: '总公司',
          value: '0',
          children: [
            {
              title: '潍坊分公司',
              value: '1'
            },
            {
              title: '济南分公司',
              value: '2'
            },
            {
              title: '青岛分公司',
              value: '3'
            },
            {
              title: '烟台分公司',
              value: '4'
            }
          ]
        }
      ]
    }
  },
  methods: {
    // 获取用户信息
    edit (id, type) {
      this.visible = true
      if (type && type === 'add') {
        this.addable = true
        this.type = type
      }
      if (type === 'edit') {
        this.editable = true
        this.type = type
      }
      if (type === 'show') {
        this.showable = true
        this.type = type
      }
      const { form: { setFieldsValue, resetFields } } = this
      if (['edit', 'show'].includes(type)) {
        this.confirmLoading = true
        getObj(id).then(res => {
          const record = res.data
          this.dsType = record.dsType
          this.confirmLoading = false
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['roleId', 'roleName', 'roleCode', 'dsType', 'dsScopeList']))
          })
        })
      } else {
        this.dsType = '2'
        resetFields()
      }
    },
    selectChange (e) {
      this.dsType = e
    },
    handleOk () {
      const validateFieldsKey = this.dsType === '1'
        ? ['roleId', 'roleName', 'roleCode', 'dsType', 'dsScopeList']
        : ['roleId', 'roleName', 'roleCode', 'dsType']
      this.form.validateFields(validateFieldsKey, async (err, values) => {
        if (!err) {
          if (this.type === 'add') {
            await addObj(values)
          } else if (this.type === 'edit') {
            await putObj(values)
          }
          this.confirmLoading = true
          setTimeout(() => {
            this.confirmLoading = false
            this.$emit('ok')
            this.visible = false
          }, 1500)
        }
      })
    },
    handleCancel () {
      this.visible = false
      setTimeout(() => {
        this.addable = false
        this.showable = false
        this.editable = false
      }, 200)
    }
  }
}
</script>

<style scoped>

</style>
